# lc1
if(!is.null(ABS_covariates_lab[["lc1"]])) {
	selected_covariates <- paste(ABS_covariates_lab[["lc1"]],collapse = " + ")
	formula <- paste0("lc1 ~ absenteeism + ", 
										selected_covariates, 
										"| Z_absenteeism + ",
										selected_covariates)
} else {
	formula <- "lc1 ~ absenteeism | Z_absenteeism"
}


formula <- as.formula(formula)
lc1_lab <- iv_robust(formula = formula, data = lab,diagnostics = T)

lc1_lab_pvals <- getParametricPValue(model = lc1_lab,
																		 hypothesis = "upr")

#pta

if(!is.null(ABS_covariates_lab[["pta"]])) {
	selected_covariates <- paste(ABS_covariates_lab[["pta"]],collapse = " + ")
	formula <- paste0("pta ~ absenteeism + ", 
										selected_covariates, 
										"| Z_absenteeism + ",
										selected_covariates)
} else {
	formula <- "pta ~ absenteeism | Z_absenteeism"
}

formula <- as.formula(formula)

pta_lab <- iv_robust(formula = formula , data = lab)
pta_lab_pvals <- getParametricPValue(model = pta_lab,
																		 hypothesis = "upr")

# bring_up
if(!is.null(ABS_covariates_lab[["bring_up"]])) {
	selected_covariates <- paste(ABS_covariates_lab[["bring_up"]],collapse = " + ")
	formula <- paste0("bring_up ~ absenteeism + ", 
										selected_covariates, 
										"| Z_absenteeism + ",
										selected_covariates)
} else {
	formula <- "bring_up ~ absenteeism | Z_absenteeism"
}

formula <- as.formula(formula)

bring_up_lab <- iv_robust(formula = formula , data = lab)
bring_up_lab_pvals <- getParametricPValue(model = bring_up_lab,
																		 hypothesis = "upr")

#assemble
if(!is.null(ABS_covariates_lab[["assemble"]])) {
	selected_covariates <- paste(ABS_covariates_lab[["assemble"]],collapse = " + ")
	formula <- paste0("assemble ~ absenteeism + ", 
										selected_covariates, 
										"| Z_absenteeism + ",
										selected_covariates)
} else {
	formula <- "assemble ~ absenteeism | Z_absenteeism"
}

formula <- as.formula(formula)

assemble_lab <- iv_robust(formula = formula , data = lab)
assemble_lab_pvals <- getParametricPValue(model = assemble_lab,
																					hypothesis = "upr")

# absenteeism_action
if(!is.null(ABS_covariates_lab[["absenteeism_action"]])) {
	selected_covariates <- paste(ABS_covariates_lab[["absenteeism_action"]],collapse = " + ")
	formula <- paste0("absenteeism_action ~ absenteeism + ", 
										selected_covariates, 
										"| Z_absenteeism + ",
										selected_covariates)
} else {
	formula <- "absenteeism_action ~ absenteeism | Z_absenteeism"
}

formula <- as.formula(formula)
absenteeism_action_lab <- iv_robust(formula = formula , data = lab)
absenteeism_action_lab_pvals <- getParametricPValue(model = absenteeism_action_lab,
																					hypothesis = "upr")

#setup
control_means <- with(
	lab, 
	c(
		round(mean(lc1[Z_absenteeism == 0],na.rm = TRUE), 2),
		round(mean(bring_up[Z_absenteeism == 0],na.rm = TRUE), 2),
		round(mean(pta[Z_absenteeism == 0],na.rm = TRUE), 2),
		round(mean(assemble[Z_absenteeism == 0],na.rm = TRUE), 2),
		round(mean(absenteeism_action[Z_absenteeism == 0],na.rm = TRUE), 2)
	)
)
control_means <- as.character(control_means)

pval_lines <- c(
	round(lc1_lab_pvals["absenteeism"],3),
	round(bring_up_lab_pvals["absenteeism"],3),
	round(pta_lab_pvals["absenteeism"],3),
	round(assemble_lab_pvals["absenteeism"],3),
	round(absenteeism_action_lab_pvals["absenteeism"],3)
)

hypothesis_lines <- c(
	"upr",
	"upr",
	"upr",
	"upr",
	"upr"
)

covariate_counts <- c(
	length(ABS_covariates_lab$lc1),
	length(ABS_covariates_lab$bring_up),
	length(ABS_covariates_lab$pta),
	length(ABS_covariates_lab$assemble),
	length(ABS_covariates_lab$absenteeism_action)
)


sink("03_tables/ABS_attitudes_lab_Z_iv_covariates.tex")
print(texreg(l = list(lc1_lab,
								bring_up_lab,
								pta_lab,
								assemble_lab,
								absenteeism_action_lab),
			 custom.coef.map = list("absenteeism" = "absenteeism"), include.rsquared = F,include.rmse = F,
			 table = F, use.packages = F, custom.gof.names = c("Adjusted R^2", "Observations"),
			 digits = 3, include.ci = FALSE,
			 override.pvalues = list(lc1_lab_pvals,
			 												bring_up_lab_pvals,
			 												pta_lab_pvals, 
			 												assemble_lab_pvals,
			 												absenteeism_action_lab_pvals),
			 stars = c(0.01, 0.05, 0.1),
			 custom.header = list("Involve LC1 Chair" = 1, 
			 										 "Tell village" = 2,
			 										 "Use PTA" = 3,
			 										 "Assemble group" = 4,
			 										 "Index" = 5),
			 custom.model.names =  c("(1)",
			 												"(2)",
			 												"(3)",
			 												"(4)",
			 												"(5)"),
			 custom.gof.rows = list(
			 	"Control Mean" = control_means,
			 	"$p$-values" = pval_lines,
			 	"Hypothesis" = hypothesis_lines,
			 	"Block FE" = c("No","No","No","No",
			 		"No"),
			 	"LASSO covariates" = covariate_counts),
			 reorder.gof = c(1,2,3,4,5,7,6)))
sink()


